<?php
/**
 * Interface for an students to select courses
 */
?>
<html>
<head>

</head>
<body>
<?php
require_once '../dbms_university_courses_mvc/Connection.php';
require_once '../dbms_university_courses_mvc/Model/model_require_once.php';

/* @var $student Student */
/* @var $studentCourseRegistration StudentCourseRegistration */
/* @var $course Course */
/* @var $coursePeriod CoursePeriod */

$students = Student::getStudents_by_profile_id ( $_SESSION ['profile_id'] );
$student = $students [0];

//get the courses this student is enrolled in
$student_course_registrations = StudentCourseRegistration::getStudentCourseRegistrations_by_student_id ( $student->get_student_id () );

//get the previous courses this student has taken
$courses_taken = array ();
$courses_enrolled_in = array ();
foreach ( $student_course_registrations as $studentCourseRegistration ) {
	$course = Course::constructFrom_course_id ( $studentCourseRegistration->get_course_id () );
	if ($studentCourseRegistration->get_grade () != NULL) {
		array_push ( $courses_taken, $course );
	} else {
		array_push ( $courses_enrolled_in, $course );
	}
}

//get the periods of the courses this student is enrolled in
$studentOccupiedPeriods = getStudentOccupiedPeriods ( $student_course_registrations );

?>
<h1>Select From Available Courses</h1>
<?php
//get all the courses are available
$courses = Course::getCourses ();

?>
<table>
	<tr>
		<th>Name</th>
		<th>Instructor</th>
		<th>Department</th>
		<th>Term</th>
		<th>Credits</th>
		<th>Classroom</th>
			</tr>
<?php
foreach ( $courses as $course ) {
	if($course->get_classroom_id () == NULL)
		continue;//skip this one
		
	/* @var $term Term */
	$term_id = $course->get_term_id ();
	if (isset ( $term_id )) {
		$term = Term::constructFrom_term_id ( $term_id );
		if (strtolower ( $term->get_term () ) != "fall" || $term->get_year () != 2009) {
			continue;
		}
	} else {
		continue;
	}
	
	$warnings = "";
	//check if the student is already enrolled in the course
	$student_enrolled_in_course = false;
	foreach ( $courses_enrolled_in as $student_course ) {
		if ($course->get_course_name () == $student_course->get_course_name ()) {
			$student_enrolled_in_course = true;
			$warnings .= " Student is enrolled in this course.";
			break;
		}
	}
	
	//check if the student has previously taken the course
	$student_prev_enrolled_in_course = false;
	foreach ( $courses_taken as $student_course ) {
		if ($course->get_course_name () == $student_course->get_course_name ()) {
			$student_prev_enrolled_in_course = true;
			$warnings .= " Student has already taken this course.";
			break;
		}
	}
	//check for conflicting periods
	//get the periods of the current course
	//check if any of them are in the list of the periods the student is enrolled in a course
	//if so, grey out, and make the title "Course periods conflict with 'course_name'"
	$conflictingCourseId = isConflictingCoursePeriods ( $course, $studentOccupiedPeriods );
	if ($conflictingCourseId) {
		$conflictingCourse = Course::constructFrom_course_id ( $conflictingCourseId );
		$warnings .= " Course periods conflict with '" . $conflictingCourse->get_course_name () . "'.";
	}
	
	//check for prerequisites
	//check if each of the prerequisites names match the name of one of the student's previous courses
	//if there is a prereq for which the student has no corresponding course
	//grey out and make the title "You are missing prerequisites"
	$missingPrerequisites = checkPrereqs ( $course, $courses_taken );
	if (count ( $missingPrerequisites ) > 0) {
		foreach ( $missingPrerequisites as $missingPrerequisite ) {
			$prereqCourse = Course::constructFrom_course_id ( $missingPrerequisite );
			$warnings .= " Missing prerequisite '" . $prereqCourse->get_course_name () . "'.";
		}
	}
	
	//check if classroom for course is full... Students registered in the course >= classroom capacity
	//get the capacity of the classroom
	/* @var $classroom Classroom */
	
	$classroom = Classroom::constructFrom_classroom_id ( $course->get_classroom_id () );
	$capacity = $classroom->get_capacity ();
	
	//count the number of students enrolled in the course
	$sql = "SELECT count(*) as num_enrolled FROM student_course_registrations GROUP BY course_id HAVING course_id = " . $course->get_course_id ();
	$result = mysql_query ( $sql );
	$result_row = mysql_fetch_array ( $result );
	$num_enrolled = $result_row ['num_enrolled'];
	
	//if the number of students enrolled >= the classroom capacity
	if ($num_enrolled >= $capacity) {
		//grey out and make the title "Course is full"
		$warnings .= " Course is full.";
	}
	
	?>
	<tr <?php
	if ($warnings != "") {
		?>
		title="<?php
		echo $warnings;
		?>"
		<?php
		if (! $student_enrolled_in_course) {
			?>
		style="color: red;" <?php
		} else {
			?> style="color: blue;"
		<?php
		}
	}
	?>
	>
		<td><?php
	echo $course->get_course_name ();
	?></td>
			<td><?php
		if ($course->get_instructor_id ()) {
			/* @var $instructor Instructor */
			$instructor = Instructor::constructFrom_instructor_id ( $course->get_instructor_id () );
			/* @var $instructor_profile Profile */
			$instructor_profile = Profile::constructFrom_profile_id ( $instructor->get_profile_id () );
			echo $instructor_profile->get_first_name () . " " . $instructor_profile->get_last_name ();
		}
		?></td>
		<td><?php
	/* @var $department Department */
	$department_id = $course->get_department_id ();
	if (isset ( $department_id )) {
		$department = Department::constructFrom_department_id ( $department_id );
		echo $department->get_department_name ();
	}
	?></td>
		<td><?php
	if (isset ( $term_id )) {
		echo $term->get_term () . " " . $term->get_year ();
	}
	?></td>
		<td><?php
	echo $course->get_credits ();
	?></td>
		<td><?php
	echo $course->get_classroom_id ();
	?></td>
			<td>
		<input type="button"
			onclick="openDialog('views/course_periods.php?course_id=<?php echo $course->get_course_id();?>');"
			value="Periods"
		/>
		</td>
		<td>
		<?php
	if (! $student_enrolled_in_course) {
		?>
		<input type="button"
			onclick="insertNewStudentCourseRegistration(<?php
		echo $student->get_student_id ();
		?>, <?php
		echo $course->get_course_id ();
		?>, refreshPage);"
			value="Enroll" <?php
		if ($warnings != "") {
			?>
			style="display: none;" <?php
		}
		?>
		/>
		<?php
		if ($warnings != "") {
			echo $warnings;
		}
		?>
		<?php
	} else {
		?>
		<input type="button" onclick="location.hash = '8'" value="Edit" />
		<?php
	}
	?>
		</td>
	</tr>
	
	<?php
}

?>
</table>


</body>
</html>
<?php

/**
 *
 * @param unknown_type $student_course_registrations
 * @return array of day_id, period_id, course_id for which the student is occupied
 * 
 */
function getStudentOccupiedPeriods($student_course_registrations) {
	/* @var $studentCourseRegistration StudentCourseRegistration */
	/* @var $course Course */
	/* @var $coursePeriod CoursePeriod */
	$studentPeriods = array ();
	foreach ( $student_course_registrations as $studentCourseRegistration ) {
		if ($studentCourseRegistration->get_grade () == NULL) {
			$course = Course::constructFrom_course_id ( $studentCourseRegistration->get_course_id () );
			$coursePeriods = CoursePeriod::getCoursePeriods_by_course_id ( $course->get_course_id () );
			foreach ( $coursePeriods as $coursePeriod ) {
				array_push ( $studentPeriods, array ($coursePeriod->get_day_id (), $coursePeriod->get_period_id (), $course->get_course_id () ) );
			}
		}
	}
	return $studentPeriods;
}

/**
 * Traverses the list of occupied periods to determine if any conflict
 *
 * @param Course $course
 * @param array $studentOccupiedPeriods
 * @return int false, or the course_id of the confliction course
 */
function isConflictingCoursePeriods($course, $studentOccupiedPeriods) {
	/* @var $studentCourseRegistration StudentCourseRegistration */
	/* @var $course Course */
	/* @var $coursePeriod CoursePeriod */
	$coursePeriods = CoursePeriod::getCoursePeriods_by_course_id ( $course->get_course_id () );
	/* @var $coursePeriod CoursePeriod */
	foreach ( $coursePeriods as $coursePeriod ) {
		for($i = 0; $i < count ( $studentOccupiedPeriods ); $i ++) {
			if ($coursePeriod->get_day_id () == $studentOccupiedPeriods [0] && $coursePeriod->get_period_id () == $studentOccupiedPeriods [1]) {
				return $studentOccupiedPeriods [2];
			}
		}
	}
	return false;
}

/**
 * Enter description here...
 *
 * @param unknown_type $course
 * @param unknown_type $courses_taken
 * @return array of missing prereqs
 */
function checkPrereqs($course, $courses_taken) {
	$coursePrereqs = CoursePrerequisite::getCoursePrerequisites_by_course_id ( $course->get_course_id () );
	$missingPrereqs = array ();
	/* @var $coursePrereq CoursePrerequisite */
	foreach ( $coursePrereqs as $coursePrereq ) {
		$prereqCourse = Course::constructFrom_course_id ( $coursePrereq->get_prerequisite_course_id () );
		$found = false;
		foreach ( $courses_taken as $course_taken ) {
			if ($course_taken->get_course_name () == $prereqCourse->get_course_name ()) {
				$found = true;
				break;
			}
		}
		if (! $found) {
			array_push ( $missingPrereqs, $prereqCourse->get_course_id () );
		}
	}
	
	return $missingPrereqs;
}

?>